﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.Revit;
using HYFamilyLibDataManager;

namespace YArchitech.HVAC
{
	public class ILayForm
	{
		public ILayForm(Document doc)
		{
			this.sfdr = new SpaceFamilyDataReader();
		}

		public FamilySymbol LayAirTerminalFormShow(ExternalCommandData commandData, string categoryName)
		{
			FamilySymbol result = null;
			LayAirTerminalFormNew layAirTerminalFormNew = new LayAirTerminalFormNew(commandData);
			if (layAirTerminalFormNew.ShowDialog() == DialogResult.OK)
			{
				this.BeSideLay = layAirTerminalFormNew.BeSideTerminal;
				this.SideTerminalWidth = layAirTerminalFormNew.CheckEndWidth;
				this.rectLayParas = layAirTerminalFormNew.rectLayParas;
				result = layAirTerminalFormNew.fs;
				this.isOnFace = layAirTerminalFormNew.isOnFace;
				if (5 == this.isOnFace)
				{
					this.planeParam = layAirTerminalFormNew.planeParameter;
				}
				this.airTerminalParaNew = layAirTerminalFormNew.airTerminalParaNew;
				this.layTerminalType = layAirTerminalFormNew.layType;
				if (this.layTerminalType == 1)
				{
					this.layLineParas = layAirTerminalFormNew.layLineParam;
				}
				else if (this.layTerminalType == 3)
				{
					this.layAreaParas = layAirTerminalFormNew.layAreaParam;
				}
				else if (this.layTerminalType == 4)
				{
					this.layMidParas = layAirTerminalFormNew.layMidParam;
				}
				else if (this.layTerminalType == 5)
				{
					if (this.BeSideLay)
					{
						this.laySideParam = layAirTerminalFormNew.laySideParam;
					}
					else
					{
						this.layAutoParas = layAirTerminalFormNew.layAutoParam;
					}
				}
			}
			return result;
		}

		private bool selSpaceGetAirV(string strType, ExternalCommandData commamdData, ref double airFlowVolume)
		{
			Selection selection = commamdData.Application.ActiveUIDocument.Selection;
			UIDocument activeUIDocument = commamdData.Application.ActiveUIDocument;
			Transaction transaction = new Transaction(commamdData.Application.ActiveUIDocument.Document, "获取空间");
			try
			{
				transaction.Start();
				Reference reference = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, "请选择空间");
				Element element = activeUIDocument.Document.GetElement(reference);
				while (element.GetType() != typeof(Space))
				{
					reference = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, "请选择空间");
					element = activeUIDocument.Document.GetElement(reference);
				}
				Space space = element as Space;
				if (!(strType == "送风口"))
				{
					if (!(strType == "排风口"))
					{
						if (strType == "回风口")
						{
							airFlowVolume = space.DesignExhaustAirflow * 28.3168465932 * 3.6;
						}
					}
					else
					{
						airFlowVolume = space.DesignReturnAirflow * 28.3168465932 * 3.6;
					}
				}
				else
				{
					airFlowVolume = space.DesignSupplyAirflow * 28.3168465932 * 3.6;
				}
				airFlowVolume = Math.Round(airFlowVolume, 2);
				transaction.Commit();
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				transaction.RollBack();
				return false;
			}
			return true;
		}

		public FamilySymbol LayDuctAccessoryFormShow(Document doc, string categoryName)
		{
			FamilySymbol result = null;
			this.tv = this.sfdr.GetAllNameAndImage2DByCategory("DuctValve", 0);
			DuctAccessoryForm ductAccessoryForm = new DuctAccessoryForm(this.tv, doc, "duct", "lay", "DuctValve");
			if (ductAccessoryForm.ShowDialog() == DialogResult.OK)
			{
				result = ductAccessoryForm.fs;
			}
			return result;
		}

		public FamilySymbol LayPipeAccessoryFormShow(Document doc, string categoryName, string chooseType, bool beSelect = false)
		{
			FamilySymbol result = null;
			this.tv = this.sfdr.GetAllNameAndImage2DByCategory("PipeValve", 0);
			DuctAccessoryForm ductAccessoryForm;
			if (beSelect)
			{
				ductAccessoryForm = new DuctAccessoryForm(this.tv, doc, "pipe", chooseType, "PipeValve", beSelect);
			}
			else
			{
				ductAccessoryForm = new DuctAccessoryForm(this.tv, doc, "pipe", chooseType, "PipeValve");
			}
			if (ductAccessoryForm.ShowDialog() == DialogResult.OK)
			{
				result = ductAccessoryForm.fs;
			}
			return result;
		}

		public FamilySymbol LayAffordPipeAccessoryFormShow(Document doc)
		{
			FamilySymbol result = null;
			this.tv = this.sfdr.GetAllNameAndImage2DByCategory("Accessory", 3);
			DuctAccessoryForm ductAccessoryForm = new DuctAccessoryForm(this.tv, doc, "PipeAccessories", "lay", "Accessory");
			if (ductAccessoryForm.ShowDialog() == DialogResult.OK)
			{
				result = ductAccessoryForm.fs;
			}
			return result;
		}

		public FamilySymbol LayAffordGPipeAccessoryFormShow(Document doc)
		{
			FamilySymbol result = null;
			this.tv = this.sfdr.GetAllNameAndImage2DByCategory("Accessory", 1);
			DuctAccessoryForm ductAccessoryForm = new DuctAccessoryForm(this.tv, doc, "GPipeAccessories", "lay", "Accessory");
			if (ductAccessoryForm.ShowDialog() == DialogResult.OK)
			{
				result = ductAccessoryForm.fs;
			}
			return result;
		}

		public FamilySymbol LayAffordPPipeAccessoryFormShow(Document doc)
		{
			FamilySymbol result = null;
			this.tv = this.sfdr.GetAllNameAndImage2DByCategory("Accessory", 2);
			DuctAccessoryForm ductAccessoryForm = new DuctAccessoryForm(this.tv, doc, "PPipeAccessories", "lay", "Accessory");
			if (ductAccessoryForm.ShowDialog() == DialogResult.OK)
			{
				result = ductAccessoryForm.fs;
			}
			return result;
		}

		public FamilySymbol LayDuctEquipmentFormShow(Document doc, string categoryName)
		{
			FamilySymbol result = null;
			this.tv = this.hvacDR.GetFamilyAllInfoTree(categoryName);
			LayDuctEquipmentForms layDuctEquipmentForms = new LayDuctEquipmentForms(this.tv, doc);
			if (layDuctEquipmentForms.ShowDialog() == DialogResult.OK)
			{
				result = layDuctEquipmentForms.fs;
				this.equipmentPara = layDuctEquipmentForms.equipmentPara;
			}
			return result;
		}

		public FamilySymbol LayFireHydrantFormShow(Document doc, string categoryName)
		{
			FamilySymbol result = null;
			this.tv = this.plumbDR.GetFamilyAllInfoTree(categoryName);
			LayFireHydrantForms layFireHydrantForms = new LayFireHydrantForms(this.tv, doc, "消火栓");
			if (layFireHydrantForms.ShowDialog() == DialogResult.OK)
			{
				result = layFireHydrantForms.fs;
				this.equipmentPara = layFireHydrantForms.equipmentPara;
			}
			return result;
		}

		public FamilySymbol LayWaterTanksFormShow(Document doc, string categoryName)
		{
			FamilySymbol result = null;
			this.tv = this.plumbDR.GetFamilyAllInfoTree(categoryName);
			LayFireHydrantForms layFireHydrantForms = new LayFireHydrantForms(this.tv, doc, "水箱");
			if (layFireHydrantForms.ShowDialog() == DialogResult.OK)
			{
				result = layFireHydrantForms.fs;
				this.equipmentPara = layFireHydrantForms.equipmentPara;
			}
			return result;
		}

		public FamilySymbol LayWaterPumpFormShow(Document doc, string categoryName)
		{
			FamilySymbol result = null;
			this.tv = this.plumbDR.GetFamilyAllInfoTree(categoryName);
			LayFireHydrantForms layFireHydrantForms = new LayFireHydrantForms(this.tv, doc, "水泵");
			if (layFireHydrantForms.ShowDialog() == DialogResult.OK)
			{
				result = layFireHydrantForms.fs;
				this.equipmentPara = layFireHydrantForms.equipmentPara;
			}
			return result;
		}

		public FamilySymbol LayDuctEquipmentCoilFormShow(Document doc, string categoryName, LayToSelectionParameter layParameter)
		{
			this.IsSelection = false;
			FamilySymbol result = null;
			DuctEquipmentCoilForms ductEquipmentCoilForms = new DuctEquipmentCoilForms(this.lstItem, doc, categoryName);
			DialogResult dialogResult = ductEquipmentCoilForms.ShowDialog();
			this.equipmentCoilSetDetailParas = ductEquipmentCoilForms.equipmentCoilSetDetailParas;
			this.eCoilParam = ductEquipmentCoilForms.eCoilParam;
			this.hongYe = ductEquipmentCoilForms.hongYe;
			if (dialogResult == DialogResult.OK)
			{
				result = ductEquipmentCoilForms.fs;
				this.IsDBFamily = ductEquipmentCoilForms.IsDbFamily;
				this.isOnFace = ductEquipmentCoilForms.isOnFace;
				if (5 == this.isOnFace)
				{
					this.planeParam = ductEquipmentCoilForms.planeParameter;
				}
				this.equipmentCoilPara = ductEquipmentCoilForms.equipmentCoilPara;
				this.layTerminalType = ductEquipmentCoilForms.layType;
				if (this.layTerminalType == 1)
				{
					this.layLineParas = ductEquipmentCoilForms.layLineParam;
				}
				else if (this.layTerminalType == 3)
				{
					this.layAreaParas = ductEquipmentCoilForms.layAreaParam;
				}
				else if (this.layTerminalType == 4)
				{
					this.layMidParas = ductEquipmentCoilForms.layMidParam;
				}
				this.rectLayParas = ductEquipmentCoilForms.RectLayParas;
			}
			else if (dialogResult == DialogResult.Ignore)
			{
				this.IsSelection = true;
			}
			return result;
		}

		public FamilySymbol LaySprinklersFormShow(Document doc, string categoryName)
		{
			FamilySymbol result = null;
			this.tv = this.plumbDR.GetFamilyAllInfoTree(categoryName);
			LaySprinklersForms laySprinklersForms = new LaySprinklersForms(this.tv, doc);
			if (laySprinklersForms.ShowDialog() == DialogResult.OK)
			{
				result = laySprinklersForms.fs;
				this.sprinklersPara = laySprinklersForms.sprinklersPara;
				this.layTerminalType = laySprinklersForms.layType;
				if (this.layTerminalType == 1)
				{
					this.layLineParas = laySprinklersForms.layLineParam;
				}
				else if (this.layTerminalType == 3)
				{
					this.layAreaParas = laySprinklersForms.layAreaParam;
				}
				else if (this.layTerminalType == 4)
				{
					this.layMidParas = laySprinklersForms.layMidParam;
				}
			}
			return result;
		}

		public FamilySymbol LayPlumbingFixturesFormShow(Document doc, string categoryName)
		{
			FamilySymbol result = null;
			this.tv = this.plumbDR.GetFamilyAllInfoTree(categoryName);
			LayPlumbingFixturesForms layPlumbingFixturesForms = new LayPlumbingFixturesForms(this.tv, doc);
			if (layPlumbingFixturesForms.ShowDialog() == DialogResult.OK)
			{
				result = layPlumbingFixturesForms.fs;
				this.isOnFace = layPlumbingFixturesForms.isOnFace;
				if (5 == this.isOnFace)
				{
					this.planeParam = layPlumbingFixturesForms.planeParameter;
				}
				this.plumbingFixturesPara = layPlumbingFixturesForms.plumbingFixturesPara;
				this.layTerminalType = layPlumbingFixturesForms.layType;
				if (this.layTerminalType == 1)
				{
					this.layLineParas = layPlumbingFixturesForms.layLineParam;
				}
				else if (this.layTerminalType == 3)
				{
					this.layAreaParas = layPlumbingFixturesForms.layAreaParam;
				}
				else if (this.layTerminalType == 4)
				{
					this.layMidParas = layPlumbingFixturesForms.layMidParam;
				}
			}
			return result;
		}

		public FamilySymbol LayDuctFittingFormShow(Document doc, string categoryName)
		{
			FamilySymbol result = null;
			this.tv = this.hvacDR.GetFamilyAllInfoTree(categoryName);
			LayDuctFittingForms layDuctFittingForms = new LayDuctFittingForms(this.tv, doc, categoryName);
			DialogResult dialogResult = layDuctFittingForms.ShowDialog();
			if (dialogResult == DialogResult.OK)
			{
				this.ductFittingFlag = 1;
				result = layDuctFittingForms.fs;
			}
			else if (dialogResult == DialogResult.Retry)
			{
				this.ductFittingFlag = 0;
				result = layDuctFittingForms.fs;
			}
			return result;
		}

		public FamilySymbol LayPipeFittingFormShow(Document doc, string categoryName)
		{
			FamilySymbol result = null;
			this.tv = this.hvacDR.GetFamilyAllInfoTree(categoryName);
			LayPipeFittingForms layPipeFittingForms = new LayPipeFittingForms(this.tv, doc, categoryName);
			DialogResult dialogResult = layPipeFittingForms.ShowDialog();
			if (dialogResult == DialogResult.OK)
			{
				this.pipeFittingFlag = 1;
				result = layPipeFittingForms.fs;
			}
			else if (dialogResult == DialogResult.Retry)
			{
				this.pipeFittingFlag = 0;
				result = layPipeFittingForms.fs;
			}
			return result;
		}

		public FamilySymbol LayRadiatorFormShow(Document doc, string categoryName)
		{
			FamilySymbol result = null;
			this.tv = this.hvacDR.GetFamilyAllInfoTree(categoryName);
			LayRadiatorForms layRadiatorForms = new LayRadiatorForms(this.tv, doc);
			if (layRadiatorForms.ShowDialog() == DialogResult.OK)
			{
				result = layRadiatorForms.fs;
				this.radiatorPara = layRadiatorForms.radiatorPara;
				this.layRadiatorType = layRadiatorForms.layType;
				if (this.layRadiatorType == 1)
				{
					this.layLineParas = layRadiatorForms.layLineParam;
				}
				else if (this.layRadiatorType == 2)
				{
					this.layWindowParas = layRadiatorForms.layWindowParam;
				}
			}
			return result;
		}

		public void YJKPipingFittingSetForm(Document doc, string message)
		{
			new FormPipingFittingSet(doc, this.hvacDR, message).ShowDialog();
		}

		private Dictionary<int, HYPropertys> dictionaryTreeProperty = new Dictionary<int, HYPropertys>();

		private TreeView tv = new TreeView();

		private List<DbFamily> DbFamilyList = new List<DbFamily>();

		private List<ListItem> lstItem = new List<ListItem>();

		private HVACDataReader hvacDR;

		private PlumbingDataReader plumbDR;

		private SpaceFamilyDataReader sfdr;

		public bool IsSelection;

		public bool IsDBFamily;

		public LayAreaParameter layAreaParas;

		public LayLineParameter layLineParas;

		public LayWindowParameter layWindowParas;

		public LaySideParameter laySideParam;

		public LayMidParameter layMidParas;

		public LayAutoParameter layAutoParas;

		public AirTerminalParam airTerminalPara;

		public AirTerminalParamNew airTerminalParaNew;

		public SprinklersParam sprinklersPara;

		public EquipmentCoilParam equipmentCoilPara;

		public EquipmentCoilSetDetailParas equipmentCoilSetDetailParas;

		public EquipmentParam equipmentPara;

		public PlumbingFixturesParam plumbingFixturesPara;

		public RadiatorParam radiatorPara;

		public bool BeSideLay;

		public double SideTerminalWidth;

		public EquipmentCoilParameter eCoilParam;

		public RectLayParamers rectLayParas;

		public int ductFittingFlag;

		public int pipeFittingFlag;

		public int layTerminalType;

		public int layRadiatorType;

		public int isOnFace;

		public ListItemPlane planeParam;

		public bool bIsShowAirTeminalZone;

		public int nZoneType;

		public double dRadius;

		public double dWidth;

		public double dLength;

		public bool hongYe;
	}
}
